﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Fish.Models;
using Fish.DAL;

namespace Fish.Controllers
{
    /*Creates and edits customzied tabs
     */
    [Authorize]
    public class CustomizedController : Controller
    {
        private FishContext db = new FishContext();

        // GET: /Customized/
        public ActionResult Index()
        {
            return RedirectToAction("AccessDenied", "Error");
            return View(db.Customizeds.ToList());
        }

        // GET: /Customized/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("PageNotFound", "Error");

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customizeds customizeds = db.Customizeds.Find(id);
            if (customizeds == null)
            {
                return RedirectToAction("Expired", "Error");

                return HttpNotFound();
            }
            return View(customizeds);
        }

        // GET: /Customized/Create
        public ActionResult Create(int TemplateId = -1, int  Tab = -1)
        {
            
            if (db.Templates.Find(TemplateId) != null && db.Templates.Find(TemplateId).Customized.Count > Tab)
            {
                Session["tabId"] = Tab+"";
                Customizeds cus = db.Templates.Find(TemplateId).Customized.ElementAt(Tab);
                @ViewBag.TabName = cus.Name; 
                int eventID = int.Parse(Session["eventID"].ToString());
                string passedArray = "";
                for (int i = 0; i < db.Events.Find(eventID).customStrings.Count(); i++)
                {
                    if (db.Events.Find(eventID).customStrings.ElementAt(i).tabNumber==Tab) {
                        if (passedArray == "")
                        {
                            passedArray = passedArray + db.Events.Find(eventID).customStrings[i].text;
                        }
                        else { 
                        passedArray=passedArray + ","+ db.Events.Find(eventID).customStrings[i].text ;
                        }
                    }
                }
                for (int i = 0; i < db.Events.Find(eventID).customStrings.Count(); i++)
                {
                    System.Diagnostics.Debug.WriteLine("Hopa: " + db.Events.Find(eventID).customStrings.ElementAt(i).text);
                }
                    @ViewBag.passedArraySent = passedArray;
                return View(cus);
            }
            else
            {
                return View("NF");
            }
        }

        // POST: /Customized/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Customizeds customizeds)
        {
                db.Customizeds.Add(customizeds);
                db.SaveChanges();
                return RedirectToAction("Index");
        }

        // GET: /Customized/Edit/5
        public ActionResult Edit(int? id)
        {

            return RedirectToAction("AccessDenied", "Error");
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customizeds customizeds = db.Customizeds.Find(id);
            if (customizeds == null)
            {
                return HttpNotFound();
            }
            return View(customizeds);
        }

        // POST: /Customized/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,Name,Created,Modified")] Customizeds customizeds)
        {

            return RedirectToAction("AccessDenied", "Error");
            if (ModelState.IsValid)
            {
                db.Entry(customizeds).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customizeds);
        }

        // GET: /Customized/Delete/5
        public ActionResult Delete(int? id)
        {

            return RedirectToAction("AccessDenied", "Error");
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customizeds customizeds = db.Customizeds.Find(id);
            if (customizeds == null)
            {
                return HttpNotFound();
            }
            return View(customizeds);
        }

        // POST: /Customized/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {

            return RedirectToAction("AccessDenied", "Error");
            Customizeds customizeds = db.Customizeds.Find(id);
            db.Customizeds.Remove(customizeds);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public String SaveCustomized(String[] customized, String cusNumber)
        {
            System.Diagnostics.Debug.WriteLine(cusNumber);
            int eventID = int.Parse(Session["eventID"].ToString());
            Events events = db.Events.Find(eventID);
            Templates template = events.Template;
            
            int tabId = int.Parse(Session["tabId"].ToString());
            int counter = 0;
            for (int i = 0; i < tabId; i++) {
                counter += template.Customized.ElementAt(i).fieldTitle.Count();
            }
            int counter2 = counter;
            //for (int i = 0; i < template.Customized.ElementAt(tabId).fieldTitle.Count(); i++)
            //{
            //    if (counter2 < events.customStrings.Count()) { 
            //    events.customStrings.eleClear(counter2);
            //    counter2++;
            //    }
            //}
            //db.SaveChanges();
            for (int i = 0; i < customized.Count(); i++)
                {
                    System.Diagnostics.Debug.WriteLine(customized[i]);
                    //customizedString temp = new customizedString();
                    //temp.text = customized[i];
                    //temp.tabNumber = index;
                    events.customStrings.ElementAt(counter).text = customized[i];
                    counter++;
                   // events.customStrings.Add(temp);
                    db.Entry(events).State = EntityState.Modified;
                }
                db.SaveChanges();

           // System.Diagnostics.Debug.WriteLine(db.Events.Find(eventID).customStrings.ElementAt(index).text);
            return "/./EventInfo/Details/"+Session["eventID"];
                      }
    }
}
